package config

import (
	"common/kms"
	"common/report"
	"fmt"
	"github.com/spf13/viper"
	"os"
	"path"
	"time"
)

type Config struct {
	CenterUrl     string        `help:"卫星地址" devDefault:"estore.guanjieai.com:10000" default:"estore.guanjieai.com:10000"`
	Timeout       time.Duration `help:"上传下载的超时时间" default:"10m"`
	P2PTimeout    time.Duration `help:"p2p 打洞超时" default:"20s"`
	Parallel      bool          `help:"并发上传下载(暂时未实现)" default:"false"`
	MaxSegment    int           `help:"最大分段大小" default:"20000000"`
	UseP2P        bool          `help:"是否使用p2p" default:"false"`
	UseKms        bool          `help:"是否使用kms" default:"false"`
	Kms           kms.Config
	UseReport     bool `help:"是否上报上传下载数据" default:"false"`
	Report        report.Config
	DefaultBucket string `help:"默认bucket" default:"default"`
	ApiKey        string `help:"字符串形式的apikey"`
	DebugDevice   string `help:"DebugDevice" default:""`
}

// ReadUploadSpeedFromFile 读每个切片上传速度，单位为字节/秒，网速/8
func ReadUploadSpeedFromFile() int64 {
	const f = 1000 / 8 //换算参数
	var defaultSpeed int64 = 2000 * f
	home, err := os.UserHomeDir()
	if err != nil {
		return defaultSpeed
	}
	cfgFile := path.Join(home, "speed.yaml")
	v := viper.New()
	v.SetConfigType("yaml")
	v.SetConfigFile(cfgFile)
	err = v.ReadInConfig()
	if err != nil {
		return defaultSpeed
	}
	up := v.GetInt64("upload") //读出来的是 kpbs
	if up > 0 {
		defaultSpeed = up * f
		fmt.Println("read upload speed ", up)
	}

	return defaultSpeed
}
